﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using KerriganDirection.DirectionServiceWCF;
using System.ComponentModel;
using KerriganDirection.Helpers;

namespace KerriganDirection.SessionMgt
{
    /// <summary>
    /// Interaction logic for AddSession.xaml
    /// </summary>
    public partial class ucAddSession : UserControl , INotifyPropertyChanged
    {
        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion

        #region Properties

        private MainModule _MainModuleVO;
        public MainModule MainModuleVO
        {
            get
            {
                return this._MainModuleVO;
            }
            set
            {
                this._MainModuleVO = value;
                OnPropertyChanged("MainWindowVO");
            }
        }

        #endregion

        #region Methods

        public ucAddSession()
        {
            this.MainModuleVO = (App.Current.Windows[0] as MainWindow).MainModuleVO;
            
            InitProperties();
            
            InitializeComponent();

            //Add default value
            this.MainModuleVO.SessionModuleVO.SessionVO.Date = DateTime.Today.AddHours(Double.Parse(this.TimeIUD.Value.ToString()));
            this.dpDate.SelectedDate = this.MainModuleVO.SessionModuleVO.SessionVO.Date;    

            LoadComboBox();
        }

        public void InitProperties()
        {
            //Initialization Virtual Object
            this.MainModuleVO.SessionModuleVO.SessionVO = new SessionDC();
            this.MainModuleVO.SessionModuleVO.SessionVO.Agency = new AgencyDC();
            this.MainModuleVO.SessionModuleVO.SessionVO.Formation = new FormationDC();
            this.MainModuleVO.SessionModuleVO.SessionVO.Instructor = new InstructorDC();
            this.MainModuleVO.SessionModuleVO.SessionVO.LevelSession = new LevelSessionDC();
            this.MainModuleVO.SessionModuleVO.SessionVO.FormationCategoryDC = new FormationCategoryDC();
            this.MainModuleVO.SessionModuleVO.SessionVO.Vehicle = new VehicleDC();
        }

        public void LoadComboBox()
        {
            //DataContext
            this.DataContext = this.MainModuleVO.SessionModuleVO.SessionVO;
            this.cbLevelSession.DataContext = this.MainModuleVO.SessionModuleVO.SessionVO.LevelSession;
            this.cbInstructor.DataContext = this.MainModuleVO.SessionModuleVO.SessionVO.Instructor;
            this.cbLieu.DataContext = this.MainModuleVO.SessionModuleVO.SessionVO.Agency;
            this.cbVehicle.DataContext = this.MainModuleVO.SessionModuleVO.SessionVO.Vehicle;
            this.cbFormation.DataContext = this.MainModuleVO.SessionModuleVO.SessionVO.Formation;

            //ItemsSource
            this.cbCategory.ItemsSource = this.MainModuleVO.SessionModuleVO.ListFormationCategoryVO;
            this.cbLevelSession.ItemsSource = this.MainModuleVO.SessionModuleVO.ListLevelsSessionVO;
            this.cbLieu.ItemsSource = this.MainModuleVO.SessionModuleVO.ListAgencyVO;
        }

        private void LoadInstructorAvailableForSession()
        {
            try
            {
                if (this.MainModuleVO.SessionModuleVO.SessionVO.EndDate > this.MainModuleVO.SessionModuleVO.SessionVO.Date)
                {
                    this.InstructorSP.Visibility = System.Windows.Visibility.Visible;
                    this.cbInstructor.ItemsSource = this.MainModuleVO.SessionModuleVO.GetListInstructorBySession();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        private void LoadVehicleAvaibableForSession()
        {
            try
            {
                if (this.MainModuleVO.SessionModuleVO.SessionVO.EndDate > this.MainModuleVO.SessionModuleVO.SessionVO.Date)
                {
                    this.VehicleSP.Visibility = System.Windows.Visibility.Visible;
                    this.MainModuleVO.SessionModuleVO.GetListVehicleAvailable();
                    this.cbVehicle.ItemsSource = this.MainModuleVO.SessionModuleVO.ListVehicleAvailableVO.Where(t => t.TypeVehicle == this.MainModuleVO.SessionModuleVO.SessionVO.Formation.FormationCategory.TypeVehicle.Name);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        private Boolean GetEndDateForSession()
        {
            if (this.MainModuleVO.SessionModuleVO.SessionVO.Formation.Id != 0)
            {
                this.MainModuleVO.SessionModuleVO.SessionVO.EndDate = HelperMethods.GetEndDate(this.MainModuleVO.SessionModuleVO.SessionVO);
                return true;
            }

            return false;
        }

        private void LoadDynamicComboBox()
        {
            if (GetEndDateForSession())
            {
                this.TimeIUD.Value = this.MainModuleVO.SessionModuleVO.SessionVO.Date.Hour;
                LoadInstructorAvailableForSession();
                LoadVehicleAvaibableForSession();
            }
        }

        #endregion

        #region Events

        private void cbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.FormationSP.Visibility = System.Windows.Visibility.Visible;

            int IdFormationCategory = int.Parse((e.Source as ComboBox).SelectedValue.ToString());
            int IdTypeVehicule = this.MainModuleVO.SessionModuleVO.ListFormationCategoryVO.Where(f => f.IdFormationCategory == IdFormationCategory).FirstOrDefault().TypeVehicle.IdTypeVehicle;

            this.MainModuleVO.SessionModuleVO.GetListFormations();
            this.MainModuleVO.SessionModuleVO.GetListVehicle();

            this.cbFormation.ItemsSource = this.MainModuleVO.SessionModuleVO.ListFormationsVO.Where(t => t.FormationCategory.IdFormationCategory == IdFormationCategory).ToList();
            this.cbVehicle.ItemsSource = this.MainModuleVO.SessionModuleVO.ListVehicleVO.Where(t => t.TypeVehicle.IdTypeVehicle == IdTypeVehicule).ToList();
        }

        private void TimeIUD_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (this.MainModuleVO != null)
            {
                SessionDC SessionLocalVO = this.MainModuleVO.SessionModuleVO.SessionVO;
                int hours = (int)e.NewValue;
                DateTime oldDate = SessionLocalVO.Date;
                DateTime newDate = new DateTime(oldDate.Year, oldDate.Month, oldDate.Day, hours, 0, 0);
                this.MainModuleVO.SessionModuleVO.SessionVO.Date = newDate;

                LoadDynamicComboBox();
            }
        }

        private void dpDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.MainModuleVO.SessionModuleVO.SessionVO.Date = (sender as DatePicker).SelectedDate.Value;
            DateTime oldDate = this.MainModuleVO.SessionModuleVO.SessionVO.Date;
            this.MainModuleVO.SessionModuleVO.SessionVO.Date = new DateTime(oldDate.Year, oldDate.Month, oldDate.Day, int.Parse(this.TimeIUD.Value.ToString()), 0, 0);
            LoadDynamicComboBox();
        }

        private void cbFormation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ComboBox).SelectedValue != null)
            {
                this.MainModuleVO.SessionModuleVO.SessionVO.Formation = this.MainModuleVO.SessionModuleVO.ListFormationsVO.Where(t => t.Id == int.Parse((sender as ComboBox).SelectedValue.ToString())).FirstOrDefault();
                LoadDynamicComboBox();

                if (this.MainModuleVO.SessionModuleVO.SessionVO.Formation.IsCircuit)
                {
                    this.LocationSP.Visibility = System.Windows.Visibility.Visible;
                    this.MainModuleVO.SessionModuleVO.GetListAgency();
                    this.cbLieu.ItemsSource = this.MainModuleVO.SessionModuleVO.ListAgencyVO;
                }
                else
                {
                    this.LocationSP.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        private void cbInstructor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ComboBox).SelectedValue != null)
            {
                this.MainModuleVO.SessionModuleVO.SessionVO.Instructor = this.MainModuleVO.SessionModuleVO.ListInstructorVO.Where(t => t.IdInstrutorDC == int.Parse((sender as ComboBox).SelectedValue.ToString())).FirstOrDefault();
            }
        }

        #endregion


    }
}
